package com.snail.common.toolUtil.fileutil;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 文件上传
 *
 * @author houyanfeng
 * @date 2021-06-15
 */
public class FileUtil {

    public static final int BUFFER_SIZE = 16 * 1024;
    public static final String ATTR_ROOT_PATH = "upload";
    public static final String ATTR_PIC_PATH = "picture";
    public static final String ATTR_TEXT_PATH = "text";
    public static final String ATTR_SUCCESS = "success";
    public static final String ATTR_MESSAGE = "message";
    public static final String ATTR_POSTFIX = "postfix";
    public static final String PICFILE_ALLOWTYPE = "gif,bmp,jpg,png,jpeg";
    public static final String TEXTFILE_ALLOWTYPE = "doc,docx,xls,xlsx,txt";
    public static final String MEDIAFILE_ALLOWTYPE = "flv";

    /**
     * 文件复制
     *
     * @param src 输入
     * @param dst 输出
     */
    public static void copy(File src, File dst) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
            out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
            byte[] buffer = new byte[BUFFER_SIZE];
            while (in.read(buffer) > 0) {
                out.write(buffer);
            }
        } finally {
            if (null != in) {
                in.close();
            }
            if (null != out) {
                out.close();
            }
        }
    }

    /**
     * 文件允许类型
     *
     * @param allowTypes 允许的后缀名
     * @param fileType   后缀名
     */
    public static boolean isAcceptFileType(String allowTypes, String fileType) {
        if (fileType == null || allowTypes == null)
            return true;
        String[] allows = allowTypes.split(",");
        if (fileType.indexOf(".") >= 0) {
            fileType = fileType.substring(fileType.indexOf(".") + 1);
        }
        for (String allow : allows) {
            allow = allow.trim();
            fileType = fileType.trim().toLowerCase();
            if (fileType.equals(allow))
                return true;
        }
        return false;
    }

    /**
     * 检查创建文件目录
     */
    public static void checkDirAndCreate(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    /**
     * 新建文件
     *
     * @param path     路径
     * @param fileName 文件名
     * @return
     */
    public static File createFile(String path, String fileName) {
        checkDirAndCreate(path);
        File proFile = new File(path + File.separator + fileName);
        return proFile;
    }

    /**
     * 上传文件
     *
     * @param srcFile     源文件
     * @param tarPath     目标文件路径
     * @param tarFileName 目标文件名
     * @throws IOException
     */
    public static void uploadFile(File srcFile, String tarPath, String tarFileName) throws IOException {
        File tarFile = createFile(tarPath, tarFileName);
        copy(srcFile, tarFile);
    }

    /**
     * 上传图片
     *
     * @param picFile            文件
     * @param picFileContentType 文件类型
     * @param picFileFileName    文件名
     */
    public static String uploadPicFile(File picFile, String picFileContentType, String picFileFileName) throws IOException {
        String postfix = getExtention(picFileFileName);
        String dirName = ATTR_ROOT_PATH + File.separator + ATTR_PIC_PATH + File.separator + new SimpleDateFormat("yyyyMM").format(new Date());
        String picPath = ATTR_ROOT_PATH + File.separator + ATTR_PIC_PATH + File.separator + new SimpleDateFormat("yyyyMM").format(new Date());
        String picFileName = new Date().getTime() + postfix;
        String path = "/upload/" + dirName;
        uploadFile(picFile, path, picFileName);
        return picPath + File.separator + picFileName;
    }

    /**
     * 上传文档
     *
     * @param textFile            文件
     * @param textFileContentType 文件类型
     * @param textFileFileName    文件名
     */
    public static String uploadTextFile(File textFile, String textFileContentType, String textFileFileName) {
        String postfix = getExtention(textFileFileName);
//		if (isAcceptFileType(TEXTFILE_ALLOWTYPE, postfix)) {
        String dirName = ATTR_ROOT_PATH + File.separator + ATTR_TEXT_PATH + File.separator + new SimpleDateFormat("yyyyMM").format(new Date());
        String filePath = ATTR_ROOT_PATH + File.separator + ATTR_TEXT_PATH + File.separator + new SimpleDateFormat("yyyyMM").format(new Date());
        String textFileName = new Date().getTime() + postfix;
        String path = "/upload/" + dirName;
        try {
            uploadFile(textFile, path, textFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath + File.separator + textFileName;
    }

    public static String uploadMediaFile(File mediaPath, String mediaPathContentType, String mediaPathFileName) {
        String postfix = getExtention(mediaPathFileName);
        String dirName = ATTR_ROOT_PATH + File.separator + ATTR_TEXT_PATH + File.separator + new SimpleDateFormat("yyyyMM").format(new Date());
        String filePath = ATTR_ROOT_PATH + File.separator + ATTR_TEXT_PATH + File.separator + new SimpleDateFormat("yyyyMM").format(new Date());
        String mediaFileName = new Date().getTime() + postfix;
        String path = "/upload/" + dirName;
        try {
            uploadFile(mediaPath, path, mediaFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath + File.separator + mediaFileName;
    }

    /**
     * 得到文件扩展名
     *
     * @param fileName
     * @return 文件扩展名
     */
    public static String getExtention(String fileName) {
        int pos = fileName.lastIndexOf(".");
        if (pos < 0) {
            return fileName;
        }
        return fileName.substring(pos);
    }

    public static void main(String[] args) {

    }

}
